package tech.heptagram.staff.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

import javax.annotation.Resource;

import tech.heptagram.staff.mapper.ProcessRecordMapper;
import tech.heptagram.staff.model.ProcessRecord;
import tech.heptagram.staff.service.ProcessRecordService;
import tech.heptagram.staff.util.AssertUtils;


@Service
public class ProcessRecordServiceImpl implements ProcessRecordService {
    @Resource
    private ProcessRecordMapper processRecordMapper;

    @Override
    public void save(ProcessRecord processRecord) {
        AssertUtils.hasText(processRecord.getBpmsProcessId(), "审批实例ID不能为空");
        AssertUtils.hasText(processRecord.getApplicationUserid(), "钉钉Id不能为空");
        AssertUtils.hasText(processRecord.getApplicationName(), "人员信息不能为空");
        AssertUtils.notNull(processRecord.getApplicationDingtalkDepartmentId(), "钉钉部门Id不能为空");
        AssertUtils.hasText(processRecord.getApplicationMessage(), "申请信息不能为空");
        AssertUtils.hasText(processRecord.getApplicationCode(), "申请员工编码不能为空");
        AssertUtils.notNull(processRecord.getApplicationDepartmentId(), "部门Id不能为空");

        processRecordMapper.insert(processRecord);

    }

    @Override
    public void updateStateToStart(String bpmsProcessId) {
        AssertUtils.hasText(bpmsProcessId, "申请实例ID不能为空");
        int num = processRecordMapper.updateStateToStart(bpmsProcessId);
        if (num == 0) {
            throw new RuntimeException("没有找到相关申请信息：{bpmsProcessId=" + bpmsProcessId + "}");
        }
    }

    @Override
    public void updateStateToTerminate(String bpmsProcessId) {
        AssertUtils.hasText(bpmsProcessId, "申请实例ID不能为空");
        int num = processRecordMapper.updateStateToTerminate(bpmsProcessId);
        if (num == 0) {
            throw new RuntimeException("没有找到相关申请信息：{bpmsProcessId=" + bpmsProcessId + "}");
        }
    }

    @Override
    public void updateStateToFinish(String bpmsProcessId, Integer applicationResult) {
        AssertUtils.hasText(bpmsProcessId, "申请实例ID不能为空");
        AssertUtils.notNull(applicationResult, "结果不能为空");

        processRecordMapper.updateStateToFinish(bpmsProcessId, applicationResult);
    }

    @Override
    public void updateRejectMessage(String bpmsProcessId, String rejectMessage) {
        AssertUtils.hasText(bpmsProcessId, "申请实例ID不能为空");
        String info = StringUtils.isEmpty(rejectMessage) ? "" : rejectMessage;
        processRecordMapper.updateRejectMessage(bpmsProcessId, info);
    }

    @Override
    public ProcessRecord findByBpmsProcessId(String bpmsProcessId) {
        ProcessRecord processRecord = processRecordMapper.selectByBpmsProcessId(bpmsProcessId);
        return processRecord;
    }

    @Override
    public List<ProcessRecord> findByUserProcessTypeAndState(String staffCode, Integer processType, Integer processState) {
        AssertUtils.hasText(staffCode, "员工编码不能为空");
        AssertUtils.notNull(processType, "申请类型不能为空");
        AssertUtils.notNull(processState, "申请状态不能为空");

        ProcessRecord query = new ProcessRecord();
        query.setApplicationCode(staffCode);
        query.setProcessType(processType);
        query.setApplicationState(processState);

        return processRecordMapper.selectStaffProcessByTypeAndState2(query);
    }
}
